package com.fare.taluo.action.csairReport;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.dayatang.domain.InstanceFactory;
import com.fare.taluo.application.report.csair.CsairReportApplication;
import com.fare.taluo.application.system.email.EmailSendApplication;
import com.fare.taluo.application.system.email.ReportEmailApplication;
import com.fare.taluo.core.domain.queue.message.QueueEmailMessageEntity;
import com.fare.taluo.queue.ExcelCsairInternationlLinesReport;
import com.fare.taluo.vo.report.CsairReportVO;
import com.fare.taluo.vo.reportemail.EmailVO;
import com.fare.util.ReportProperties;
@Service
public class CsairReportAction {
	
	@Resource(name="csairReportApplication")
	private CsairReportApplication csairReportApplication;
	
	@Resource(name ="reportEmailApplication")
	private ReportEmailApplication reportEmailApplication;
	
	private List<EmailVO> emailList;
	private String email = null;
	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}
	
	public List<EmailVO> getEmailList() {
		return emailList;
	}

	public void setEmailList(List<EmailVO> emailList) {
		this.emailList = emailList;
	}

	public void execute() throws Exception{
		try{
		//获取后台要发送的Email
		if(email==null || email==""){
			emailList=reportEmailApplication.findAllEmail(1);//0表示不接受状态，1表示接受状态
			StringBuffer emailStr=new StringBuffer();
			for(EmailVO emailVo:emailList){
				emailStr.append(","+emailVo.getEmailAddress());
			}
			if(emailStr.length()>1 && emailStr !=null){
				email=emailStr.toString().substring(1);
			}
		}
		
		QueueEmailMessageEntity email = new QueueEmailMessageEntity();
		Map<String ,List<Map<String ,CsairReportVO>>> csairVo =new HashMap<String, List<Map<String,CsairReportVO>>>();
		csairVo=csairVo();
		ExcelCsairInternationlLinesReport.setExcelCsairReport(csairVo);
		String attachedFileName="南航国际航空监控报表";//附件名
		Date nows=new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String Name=sdf.format(nows);
		String strFilePath = ReportProperties.getInstance().getSavePath().endsWith("/")? ReportProperties.getInstance().getSavePath() :	ReportProperties.getInstance().getSavePath()+"/";
		//String strFilePath="C:/"; 
		String strFileName = strFilePath+"南航国际航空监控报表_"+Name+ ".xls";
		if(StringUtils.isNotBlank(getEmail())){//773405111@qq.com,763198111@qq.com,181002207@qq.com,49937310@qq.com,416428406@qq.com,627070574@qq.com,695786584@qq.com,553954594@qq.com
			email.setAttachPath(strFileName);//new String(location.getBytes("ISO8859-1"),"UTF-8")//new String(location.getBytes("UTF-8"),"ISO8859-1")
//			email.setBody("<p>您好!<p><p>&nbsp;&nbsp;已生成您需要的淘宝旗舰店与代理人差价报表，此报表抓取时间从 "+fromGrabDates+" 到 "+toGrabDates+"，详情请查看收附件。</p>");//正文
			email.setBody("<p>您好!<p><p>&nbsp;&nbsp;已生成您需要的南航国际航线监控报表，详情请查看收附件。</p>");//正文
			email.setSubject("南航国际航线监控报表_"+Name);//主题
			email.setToAddress(getEmail());
			email.setName(attachedFileName);
			InstanceFactory.getInstance(EmailSendApplication.class).save(email);
		}
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}
	
	
	public Map<String ,List<Map<String ,CsairReportVO>>> csairVo(){
		//List<List<Map<String ,CsairReportVO>>> dataAlls=new ArrayList<List<Map<String ,CsairReportVO>>>();//返回
		Map<String ,List<Map<String ,CsairReportVO>>> daltaAll=new HashMap<String ,List<Map<String ,CsairReportVO>>>();
		List<Map<String ,CsairReportVO>> data=new ArrayList<Map<String,CsairReportVO>>();
		
		List<CsairReportVO> VO_7=new ArrayList<CsairReportVO>();
		List<CsairReportVO> VO_15=new ArrayList<CsairReportVO>();
		List<CsairReportVO> VO_30=new ArrayList<CsairReportVO>();
		
		VO_7= csairReportApplication.findCsairReportVO_7();//获取未来7天所有数据
		VO_15=csairReportApplication.findCsairReportVO_15();//获取未来15天所有数据
		VO_30=csairReportApplication.findCsairReportVO_30();//获取未来30天所有数据
		
		
		//重组数据
		
		//过滤出有未来15天和未来30天和未来7天的数据
		if (VO_15.size() > 0) {
			for (int i = 0; i < VO_15.size(); i++) {
				Map<String, CsairReportVO> dataMap = new HashMap<String, CsairReportVO>();
				int flag = 9999;
				int flag2 = 9999;
				CsairReportVO csVo_15 = new CsairReportVO();
				csVo_15 = VO_15.get(i);
				String flightNo = csVo_15.getFlightNo();
				for (int k = 0; k < VO_30.size(); k++) {
					CsairReportVO csVo_30 = new CsairReportVO();
					csVo_30 = VO_30.get(k);
					if (csVo_30.getFlightNo().equals(flightNo)) {
						flag = k;
						break;
					}
				}
				for (int m = 0; m < VO_7.size(); m++) {
					CsairReportVO csVo_7 = new CsairReportVO();
					csVo_7 = VO_7.get(m);
					if (csVo_7.getFlightNo().equals(flightNo)) {
						flag2 = m;
						break;
					}
				}
				if (flag != 9999 || flag2 != 9999) {// 说明至少有一个航班号是相同的
					if (flag != 9999) {// 表示有未来30天
						dataMap.put("30", VO_30.get(flag));
					}
					if (flag2 != 9999) {// 表示有未来7天
						dataMap.put("7", VO_7.get(flag2));
					}

					dataMap.put("15", VO_15.get(i));
					data.add(dataMap);
					if (dataMap.get("30") != null && dataMap.get("30").getFlightNo() != null && !dataMap.get("30").getFlightNo().equals("")) {
						VO_30.remove(flag);// 把此数据从30那个List中删掉
					}
					if (dataMap.get("7") != null && dataMap.get("7").getFlightNo() != null && !dataMap.get("7").getFlightNo().equals("")) {
						VO_7.remove(flag2);// 把此数据从7那个List中删掉
					}
					VO_15.remove(i);// 把此数据从15那个List中删掉
					
					i--;
				}
				
			}
			
		}
		
		
		if (VO_30.size() > 0) {// 30VS7
			
			for (int n = 0; n < VO_30.size(); n++) {
				Map<String, CsairReportVO> dataMap = new HashMap<String, CsairReportVO>();
				int flagLast = 9999;
				String flightNo = VO_30.get(n).getFlightNo();
				for (int l = 0; l < VO_7.size(); l++) {
					if (VO_7.get(l).getFlightNo().equals(flightNo)) {
						flagLast = l;
						break;
					}
				}

				if (flagLast != 9999) {// 表示有相同的航班号
					dataMap.put("30", VO_30.get(n));
					dataMap.put("7", VO_7.get(flagLast));
					data.add(dataMap);

					VO_30.remove(n);
					VO_7.remove(flagLast);
					n--;
				}

			}

		}
		
		//剩下的数据只有未来7天或未来15天或未来30天的航班的数据
		if(VO_7.size()>0){
			for(CsairReportVO vo_7:VO_7){
				Map<String ,CsairReportVO> dataMap= new HashMap<String, CsairReportVO>();
				dataMap.put("7", vo_7);
				data.add(dataMap);
			}
		}
		if(VO_15.size()>0){
			for(CsairReportVO vo_15:VO_15){
				Map<String ,CsairReportVO> dataMap= new HashMap<String, CsairReportVO>();
				dataMap.put("15", vo_15);
				data.add(dataMap);
			}
		}
		if(VO_30.size()>0){
			for(CsairReportVO vo_30:VO_30){
				Map<String ,CsairReportVO> dataMap= new HashMap<String, CsairReportVO>();
				dataMap.put("30", vo_30);
				data.add(dataMap);
			}
		}
		
		//处理同一航线数据		
		if(data.size()>0){
			for(int i=0;i<data.size();i++){
				Map<String ,List<Map<String ,CsairReportVO>>> daltaAllT=new HashMap<String ,List<Map<String ,CsairReportVO>>>();
				List<Map<String ,CsairReportVO>> dataT=new ArrayList<Map<String,CsairReportVO>>();
				//用来判断航线
				String flightLine=null;
				Map<String ,CsairReportVO> MapVo=new HashMap<String,CsairReportVO>();
				MapVo=data.get(i);
				if(MapVo.size()>0){
					for(Map.Entry<String, CsairReportVO> dataMap:data.get(i).entrySet()){
						if(dataMap.getKey() !=null && !dataMap.getKey().equals("")){
							flightLine=dataMap.getValue().getFlightLine();
							break;
						}
					}
				}
				
				//判断List里面的有与flightLine相同的航线，则过滤出来(被判断航线)
				if(!flightLine.equals("") && flightLine !=null){
					for(int k=0;k<data.size();k++){
						String flightLineT=null;
						if(data.get(k).size()>0){
							for(Map.Entry<String, CsairReportVO> dataMap:data.get(k).entrySet()){
								if(!dataMap.getValue().getFlightLine().equals("") && dataMap.getValue().getFlightLine() !=null){
									flightLineT=dataMap.getValue().getFlightLine();
									break;
								}
							}
						}
						
						if(flightLine.equals(flightLineT)){
							dataT.add(data.get(k));
							daltaAllT.put(flightLine, dataT);
							data.remove(k);
							k--;
						}
						
					}
					
					daltaAll.putAll(daltaAllT);
				}
				
			}
		}
		
		//只有一单独一条航班的
		if(data.size()>0){
			for(int lastDo=0;lastDo<data.size();lastDo++){//List<Map<String ,CsairReportVO>>
				String flightNos=null;
				String flightLine=null;
				List<Map<String ,CsairReportVO>> dataL=new ArrayList<Map<String ,CsairReportVO>>();
				Map<String ,List<Map<String ,CsairReportVO>>> daltaAllT_2=new HashMap<String ,List<Map<String ,CsairReportVO>>>();
				for(Map.Entry<String, CsairReportVO> dataMap:data.get(lastDo).entrySet()){
					flightNos=dataMap.getValue().getFlightNo();
					if(flightNos !=null && flightNos !=""){
						flightLine=dataMap.getValue().getFlightLine();
						break;
					}
				}
				dataL.add(data.get(lastDo));
				daltaAllT_2.put(flightLine, dataL);
				
				daltaAll.putAll(daltaAllT_2);
			}
			
		}
		
		return daltaAll;
	}
	
	public static void main(String[] args){
		
		
		
		List<Map<String ,String >> testLists=new ArrayList<Map<String, String>>();
		Map<String ,String >  test=new HashMap<String,String>();
		List<String> testList=new ArrayList<String>();
		test.put("1", "1");
		test.put("2", "2");
		test.put("3", "3");
		test.put("4", "4");
		
		testList.add("1");
		testList.add("2");
		testList.add("3");
		testList.add("4");
		testList.add("5");
		testList.add("6");
		int i=0;
		for(int k=0;k<testList.size();k++){
			if(testList.get(k).equals("4")){
				i=k;
				testList.remove(k);
				break;
			}
		}
		System.out.println(testList.get(i));
		
	}

}
